package abaco.gui.textfield.listener;

import java.awt.Component;
import java.awt.event.FocusEvent;

import javax.swing.border.EmptyBorder;

import abaco.gui.Manager;
import abaco.gui.textfield.TextField;

public class FocusListener implements java.awt.event.FocusListener {
	protected Component component;

	public FocusListener(Component comp) {
		this.component = comp;
	}

	// Puts border round box because it has focus
	@Override
	public void focusGained(FocusEvent evt) {
		System.out.println("FocusListener.focusGained()");
		if(component instanceof TextField) {
			((TextField)component).setBorder(((TextField)component).getFocusedBorder());
		}
		Manager.getInstance().setCurrentFocused(component);
	}

	// Removes border from box, unless it is empty, tries to parse string into tree
	@Override
	public void focusLost(FocusEvent evt) {
		if(component instanceof TextField) {
			if (((TextField)component).getText().length() > 0) {
				((TextField)component).setBorder(new EmptyBorder(((TextField)component).getInsets()));
			} else {
				((TextField)component).setBorder(((TextField)component).getDefaultBorder());
				
			}
		}		
		//TODO mover esses atributos estaticos(ex.:SELECT,DESELECT, etc) para uma classe abstrata ou interface
		if(component.getBackground() == MouseListener.SELECT) {
			component.setBackground(MouseListener.DESELECT);
		}
	}
}